package io.github.talelin.latticy.common.validator;

import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.latticy.common.annotation.IsMobile;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;


public class MobileValidator implements ConstraintValidator<IsMobile, String> {

    @Override
    public void initialize(IsMobile constraintAnnotation) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isEmpty(s)) {
            return false;
        }
        String pattern = "^(1[3-9]\\d{9}$)";
        if (!Pattern.matches(pattern, s)) {
            return false;
        }
        return true;
    }
}
